package com.yami.shop.mystery.common.listener;

import cn.hutool.core.util.ObjectUtil;
import com.yami.shop.bean.enums.OrderType;
import com.yami.shop.bean.event.CancelOrderEvent;
import com.yami.shop.bean.order.CancelOrderOrder;
import com.yami.shop.mystery.common.service.ActivityPresellService;
import com.yami.shop.mystery.common.service.MysteryBoxOrderService;
import lombok.AllArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 取消订单事件
 * @author wxf
 * @date 2023/4/13 14:48
 */
@Component("mysteryBoxCancelOrderListener")
@AllArgsConstructor
public class CancelOrderListener {

    private final MysteryBoxOrderService mysteryBoxOrderService;

    private final ActivityPresellService activityPresellService;

    @EventListener(CancelOrderEvent.class)
    @Order(CancelOrderOrder.MYSTERBOX)
    public void mysteryBoxCancelOrderListener(CancelOrderEvent event) {
        String orderNumber = event.getOrder().getOrderNumber();
        // 不是秒杀订单直接返回
        if (!Objects.equals(event.getOrder().getOrderMold(), 3)) {
            return;
        }

        mysteryBoxOrderService.cancelUnpayOrderByOrderNumber(orderNumber);
    }

    /**
     * 预售活动订单取消逻辑
     * @param event
     */
    @EventListener(CancelOrderEvent.class)
    @Order(CancelOrderOrder.ACTIVITYPRESELL)
    public void activityPresellCancelOrderListener(CancelOrderEvent event) {
        if(ObjectUtil.isNotNull(event.getOrder().getActivityId())){
            activityPresellService.cancelUnpayOrderByOrder(event.getOrder().getActivityId(),event.getOrder().getOrderNumber());
        }
    }
}
